﻿using System;
using System.Text;
using System.Windows;

namespace ClassGenerator
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void btnGenerate_Click(object sender, RoutedEventArgs e)
        {
            string json = txtJson.Text;
            string usedNamespace = txtUsingNamespace.Text;
            string defaultNamespace = txtNamespace.Text;

            //remove the first "{" and the last "}"
            json = json.Replace("{", "").Replace("}", "").Replace(Environment.NewLine, "") ;

            string[] DataFields = json.Split(new string[] {",\""}, StringSplitOptions.None);

            StringBuilder sbResult = new StringBuilder();
            sbResult.AppendFormat(@"/**
*   this class is Autogenerated by ClassGenerator
*   @{0}
*   author: hanxianlong
*/", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

            //append using namespace
            sbResult.AppendLine(usedNamespace);

            //append namespace
            if (!string.IsNullOrEmpty(defaultNamespace))
            {
                sbResult.AppendLine("namespace " + defaultNamespace + Environment.NewLine + "{");
            }

            sbResult.AppendLine("[DataContract]");
            sbResult.AppendLine("public class " + txtClassName.Text);
            sbResult.AppendLine("{");

            System.Globalization.CultureInfo cultureInfo = System.Threading.Thread.CurrentThread.CurrentCulture;

            System.Globalization.TextInfo textInfo = cultureInfo.TextInfo;
            foreach (string field in DataFields)
            {
                string[] fieldInfo = field.Split(':');//split the field, we can get a name/value pairs
                
                //get the field name
                string originalFieldName = fieldInfo[0].Replace("\"", "").Trim();
                string[] fieldNameParts = originalFieldName.Split('_');
                string fieldName = "";
                foreach (string fp in fieldNameParts)
                {
                    fieldName += textInfo.ToTitleCase(fp);
                }

                string fieldType = "string";
                if (!fieldInfo[1].ToString().StartsWith("\""))
                {
                    if (fieldInfo[1].ToLower() == "true" || fieldInfo[1].ToLower() == "false")
                    {
                        fieldType = "bool";
                    }
                    else
                    {
                        fieldType = "int";
                    }
                }


                sbResult.AppendLine("//" + field);
                sbResult.AppendFormat("[DataMember(Name = \"{0}\", IsRequired = true)]", originalFieldName);
                sbResult.AppendLine();
                sbResult.AppendLine("public " + fieldType + " " + fieldName + "{ get; set;}");
                sbResult.AppendLine("");
            }

            sbResult.AppendLine("}");

            //append namespace
            if (!string.IsNullOrEmpty(defaultNamespace))
            {
                sbResult.Append("}");
            }
            txtClass.Text = sbResult.ToString();
        }
      
    }
}
